package com.android.demolayoutpages;
 
 
import com.android.demolayoutpages.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class ViewPagerAdapter extends PagerAdapter implements ViewFactory  {
	private Context context;
	Integer pics[] = { R.drawable.img_thumb1, R.drawable.img_thumb2, R.drawable.img_thumb3, R.drawable.img_thumb4,
			R.drawable.img_thumb5 };


	public ViewPagerAdapter(Context context){
		this.context = context;
	}

	@Override
	public int getCount() {
	 	return 5;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object view) {
		((ViewPager)container).removeView((View)view);
	}
	
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		//ViewHolder holder 		= new ViewHolder();
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout 			= inflater.inflate(R.layout.layout_view_pager_view, null);
		Gallery imageGallery1= (Gallery)layout.findViewById(R.id.Gallery01);
		Gallery imageGallery2= (Gallery)layout.findViewById(R.id.Gallery02);
		final ImageSwitcher imageSwitcher1= (ImageSwitcher)layout.findViewById(R.id.ImageSwitcher01);
		final ImageSwitcher imageSwitcher2= (ImageSwitcher)layout.findViewById(R.id.ImageSwitcher02);
		imageSwitcher1.setFactory(ViewPagerAdapter.this);
		imageSwitcher2.setFactory(ViewPagerAdapter.this);
		setAnimation(imageSwitcher1);
		setAnimation(imageSwitcher2);
		imageSwitcher1.setImageResource(pics[0]);
		imageSwitcher2.setImageResource(pics[0]);
		 imageGallery1.setAdapter(new com.android.demolayoutpages.ImageAdapter(context));
		imageGallery1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageSwitcher1.setImageResource(pics[arg2]);
			}
		});
		 imageGallery2.setAdapter(new com.android.demolayoutpages.ImageAdapter(context));
		imageGallery2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageSwitcher2.setImageResource(pics[arg2]);
			}
		});
		
	 
		((ViewPager)container).addView(layout);
		return layout;
	}

	private void setAnimation(ImageSwitcher imageSwitcher1) {
		 
			imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(context,
					android.R.anim.fade_in));
			imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(context,
					android.R.anim.fade_out));
			
		 
		
	}

	@Override
	public View makeView() {
		ImageView iView = new ImageView(context);
		iView.setScaleType(ImageView.ScaleType.FIT_XY);
		iView.setLayoutParams(new 
				ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		iView.setBackgroundColor(0xFF000000);
		return iView;
	}

}
